import React, { useState } from 'react';
import NumberField from '../../../../../components/common/NumberField';
import StatefulCalculationCard from '../../../../../components/common/StatefulCalculationCard';
import ExportData from '../../../../../components/common/ExportCalculateData';
import type { ResultItem } from '../../../../../../shared/types';

// 隧道长度计算类型定义
export interface TunnelEntranceExitElevationValues { 
    entranceY: number;
    entranceZ: number;
    entrancelinelenth: number;
    exitY: number;
    exitZ: number;
    exitlinelenth: number;
    slope: number;
}
export interface TunnelEntranceExitElevationResult {
    entrancelinesmileage: number;
    entranceelevation: number;  
    exitlinesmileage: number;
    exitelevation: number;
}

// 隧道长度计算初始值配置
const initialValues: TunnelEntranceExitElevationValues = {
    entranceY: 0,
    entranceZ: 0,
    entrancelinelenth: 4,
    exitY: 0,
    exitZ: 0,
    exitlinelenth: 3,
    slope: 0
};

// 隧道长度计算卡片组件配置
const tunnelEntranceElevationConfig = {
  key: 'TunnelLenth',
  label: '隧道洞口高程与里程计算',
  formKey: 'tunnel-entrance-exit-elevation-calculation',
  initialValues: initialValues,
  calculationType: 'tunnel-entrance-exit-elevation-calculation',
  resultFormatter: (data: TunnelEntranceExitElevationResult) => [
    { key: 'entrancelinesmileage', name: '入洞口明暗线里程', value: data.entrancelinesmileage, unit: 'm' },
    { key: 'entranceelevation', name: '入洞口明暗线底板高程', value: data.entranceelevation, unit: 'm' },
    { key: 'exitlinesmileage', name: '出洞口明暗线里程', value: data.exitlinesmileage, unit: 'm' },
    { key: 'exitelevation', name: '出洞口明暗线底板高程', value: data.exitelevation, unit: 'm' },
  ],
  renderFormFields: () => (
    <>
      <NumberField name="entranceY" label="入洞口里程" required step={0.001} tooltip="入洞口明暗线里程，单位为m" />
      <NumberField name="entranceZ" label="入洞口高程" required step={0.001} tooltip="入洞口高程，单位为m" />
      <NumberField name="entrancelinelenth" label="入洞口明暗线长度" required step={0.001} tooltip="入洞口明暗线长度，单位为m" />
      <NumberField name="exitY" label="出洞口里程" required step={0.001} tooltip="出洞口明暗线里程，单位为m" />
      <NumberField name="exitZ" label="出洞口高程" required step={0.001} tooltip="出洞口高程，单位为m" />
      <NumberField name="exitlinelenth" label="出洞口明暗线长度" required step={0.001} tooltip="出洞口明暗线长度，单位为m" />
      <NumberField name="slope" label="坡度" required step={0.001} tooltip="坡度，tan值" />
    </>
  )
};

// 隧道长度计算卡片组件实现
const TunnelLenthCard: React.FC = () => {
  const [lastResults, setLastResults] = useState<ResultItem[] | null>(null);
  
  return (
    <StatefulCalculationCard
      title={tunnelEntranceElevationConfig.label}
      formKey={tunnelEntranceElevationConfig.formKey}
      initialValues={tunnelEntranceElevationConfig.initialValues}
      renderFormFields={tunnelEntranceElevationConfig.renderFormFields}
      calculationType={tunnelEntranceElevationConfig.calculationType}
      resultFormatter={(data) => {
        const results = tunnelEntranceElevationConfig.resultFormatter(data);
        setLastResults(results);
        return results;
      }}
      columnCount={2} // 使用2列布局
      tableHeight={500} // 设置表格高度为500px
      exportComponent={(form) => (
        <ExportData
          siteName={tunnelEntranceElevationConfig.label}
          inputs={form.getFieldsValue()}
          results={lastResults ? Object.fromEntries(lastResults.map(r => [r.key, r.value])) : {}}
          calculationType={tunnelEntranceElevationConfig.calculationType}
        />
      )}
    />
  );
};

export default TunnelLenthCard;